<template>
  <div class="interface-info">
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="IP地址">
          <el-input v-model="searchForm.ip" placeholder="请输入IP地址" clearable></el-input>
        </el-form-item>
        <el-form-item label="接口名称">
          <el-input v-model="searchForm.interface_name" placeholder="请输入接口名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
          <el-button type="success" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column prop="ip" label="IP地址" width="140"></el-table-column>
      <el-table-column prop="interface_name" label="接口名称" width="180"></el-table-column>
      <el-table-column prop="url" label="接口地址" min-width="200"></el-table-column>
      <el-table-column prop="check_field" label="校验字段" width="120"></el-table-column>
      <el-table-column prop="expected_value" label="期望值" width="120"></el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="160"></el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchForm.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 表单对话框组件 -->
    <interface-form-dialog
      :visible.sync="dialogVisible"
      :form-data="formData"
      @success="handleSuccess"
    ></interface-form-dialog>
  </div>
</template>

<script>
import InterfaceFormDialog from './InterfaceFormDialog.vue'
import { getInterfaceList, deleteInterfaceInfo } from '../api/interface'

export default {
  name: 'InterfaceInfo',
  components: {
    InterfaceFormDialog
  },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      formData: {},
      tableData: [],
      total: 0,
      searchForm: {
        ip: '',
        interface_name: '',
        page: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取列表数据
    async fetchData() {
      this.loading = true
      try {
        const res = await getInterfaceList(this.searchForm)
        if (res.code === 200) {
          const { list, total, page, pageSize } = res.data
          this.tableData = list
          this.total = total
          this.searchForm.page = page
          this.searchForm.pageSize = pageSize
        }
      } catch (error) {
        this.$message.error('获取数据失败')
      }
      this.loading = false
    },
    // 搜索
    handleSearch() {
      this.searchForm.page = 1
      this.fetchData()
    },
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        ip: '',
        interface_name: '',
        page: 1,
        pageSize: 10
      }
      this.fetchData()
    },
    // 新增
    handleAdd() {
      this.formData = {
        ip: '',
        interface_name: '',
        url: '',
        check_field: '',
        expected_value: '',
        id: undefined
      }
      this.dialogVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.formData = { ...row }
      this.dialogVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认删除该记录吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          const res = await deleteInterfaceInfo(row.id)
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        } catch (error) {
          this.$message.error('删除失败')
        }
      })
    },
    // 表单提交成功
    handleSuccess() {
      this.dialogVisible = false
      this.fetchData()
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.fetchData()
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.searchForm.page = val
      this.fetchData()
    }
  }
}
</script>

<style scoped>
.interface-info {
  padding: 20px;
}
.search-bar {
  margin-bottom: 20px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style> 